#include "i2c.h"
#include "systimer.h"

/* i2c1接口函数 */
static void i2c1_gpio_init(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_AF);
    gpio_pin_remap_config(GPIO_SWJ_SWDPENABLE_REMAP,ENABLE);
    gpio_init(I2C1_SCL_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, I2C1_SCL_PIN);
    gpio_init(I2C1_SDA_PORT, GPIO_MODE_OUT_OD, GPIO_OSPEED_50MHZ, I2C1_SDA_PIN);
}

void i2c1_start(void)
{
    I2C1_SCL_HIGH();
    I2C1_SDA_HIGH();
    delay_us(2);
    I2C1_SDA_LOW();
    delay_us(2);
    I2C1_SCL_LOW();
    delay_us(2);
}

void i2c1_send_byte(uint8_t byte)
{
    I2C1_SCL_LOW();
    for(uint8_t i = 0; i < 8; i++)
    {
        if(byte & 0x80)
        {
            I2C1_SDA_HIGH();
        }
        else
        {
            I2C1_SDA_LOW();
        }
            
        byte <<= 1;
        I2C1_SCL_HIGH();
        delay_us(2);
        I2C1_SCL_LOW();
        delay_us(2);
    }
}

uint8_t i2c1_read_byte(void)
{
    uint8_t data = 0;
    I2C1_SCL_LOW();
	I2C1_SDA_HIGH();
	delay_us(2);
	for(uint8_t i = 0; i < 8; i++)
	{
        data <<= 1;
        I2C1_SCL_HIGH();
        delay_us(2);
		if(I2C1_SDA_READ())
        {
            data |= 0x01;
        }
		I2C1_SCL_LOW();
		delay_us(2);
	}
	return data;
}

void i2c1_ack(void)
{
    I2C1_SCL_LOW();
    I2C1_SDA_LOW();
    delay_us(2);
    I2C1_SCL_HIGH();
    delay_us(2);
    I2C1_SCL_LOW();
    delay_us(2);
}

void i2c1_nack(void)
{
    I2C1_SCL_LOW();
    I2C1_SDA_HIGH();
    delay_us(2);
    I2C1_SCL_HIGH();
    delay_us(2);
    I2C1_SCL_LOW();
    delay_us(2);
}

void i2c1_wait_ack(void)
{
    uint8_t timeout = 0;
    I2C1_SCL_LOW();
    I2C1_SDA_HIGH();
    delay_us(2);
    I2C1_SCL_HIGH();
    delay_us(2);
    while(I2C1_SDA_READ())
    {
        timeout++;
        delay_us(10);
        if(timeout > 100)
        {
            return;
        }
    }
    I2C1_SCL_LOW();
    delay_us(2);
}

void i2c1_stop(void)
{
    I2C1_SCL_LOW();
    I2C1_SDA_LOW();
    delay_us(2);
    I2C1_SCL_HIGH();
    delay_us(2);
    I2C1_SDA_HIGH();
    delay_us(2);
}

/* i2c2接口函数 */
static void i2c2_gpio_init(void)
{
    rcu_periph_clock_enable(RCU_GPIOB);
    rcu_periph_clock_enable(RCU_AF);
    gpio_init(I2C2_SCL_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, I2C2_SCL_PIN);
    gpio_init(I2C2_SDA_PORT, GPIO_MODE_OUT_PP, GPIO_OSPEED_50MHZ, I2C2_SDA_PIN);
}

void i2c2_start(void)
{
    I2C2_SCL_HIGH();
    I2C2_SDA_HIGH();
    delay_us(2);
    I2C2_SDA_LOW();
    delay_us(2);
    I2C2_SCL_LOW();
    delay_us(2);
}

void i2c2_send_byte(uint8_t byte)
{
    I2C2_SCL_LOW();
    for(uint8_t i = 0; i < 8; i++)
    {
        if(byte & 0x01)
        {
            I2C2_SDA_HIGH();
        }
        else
        {
            I2C2_SDA_LOW();
        }
            
        byte >>= 1;
        I2C2_SCL_HIGH();
        delay_us(2);
        I2C2_SCL_LOW();
        delay_us(2);
    }
}

void i2c2_ack(void)
{
    I2C2_SCL_LOW();
    I2C2_SDA_LOW();
    delay_us(2);
    I2C2_SCL_HIGH();
    delay_us(2);
    I2C2_SCL_LOW();
    delay_us(2);
}

void i2c2_nack(void)
{
    I2C2_SCL_LOW();
    I2C2_SDA_HIGH();
    delay_us(2);
    I2C2_SCL_HIGH();
    delay_us(2);
    I2C2_SCL_LOW();
    delay_us(2);
}

void i2c2_stop(void)
{
    I2C2_SCL_LOW();
    I2C2_SDA_LOW();
    delay_us(2);
    I2C2_SCL_HIGH();
    delay_us(2);
    I2C2_SDA_HIGH();
    delay_us(2);
}

void i2c_init(void)
{
    i2c1_gpio_init();
    i2c2_gpio_init();
}


